

import 'package:flutter/material.dart';
import 'package:xim/common/get_style_view.dart';
import 'package:xim/common/util/toast.dart';
import 'package:xim/config/route_config.dart';
import 'package:xim/http/apis/user/user_api.dart';
import 'package:xim/widget/divider.dart';
import 'profile_public_controller.dart';
import 'profile_public_style.dart';

class ProfilePublicPage extends GetStyleControllerView<void, ProfilePublicController, ProfilePublicStyle>{

  final String userAccount;

  ProfilePublicPage({super.key, required this.userAccount});

  @override
  Widget buildView(BuildContext context, void data) {
    return Scaffold(
      backgroundColor: style.bgColor.color,
      appBar: style.appBar,
      body: Column(
        mainAxisSize: MainAxisSize.max,
        children: [
          Container(
            color: style.fgColor.color,
            padding: EdgeInsets.only(top: 8, bottom: 18.0,left: style.horizontalPadding.left, right: style.horizontalPadding.right),
            child: style.userHeader(controller.userInfo),
          ),
          const SizedBox(height: 12,),
          optionBar(context),
        ],
      ),
    );
  }

  ///
  Widget optionBar(BuildContext context,){
    if(controller.isSelf){
      return const Column();
    }
    if(!controller.isFriend){
      return style.optionBtn(
        "添加好友",
        onTap: () async {
          UserApi.requestAddFriend(controller.userInfo.account);
          ToastUtil.toast("好友添加成功", context: context);
          Routers.back();
        },
      );
    }
    if(controller.friendModel?.status==1){
      // 正常好友
      return Column(
        children: [
          style.settingNodeCompose(),
          style.moreInfoCompose(
              onTab: ()=>Routers.toDetailProfileInfoPage(controller.userAccount)
          ),
          style.advancedCompose(),
          const SizedBox(height: 18,),
          style.optionBtn("发送消息"),
          DividerCompose.simpleDivider(context: context, paddingGap: 1,gap: 0, horizontalGap: 18),
          style.optionBtn("P2P连接"),
          const SizedBox(height: 12,),
          style.optionBtn(
            "删除好友",
            titleColor: style.deleteFriendTextColor,
            onTap: (){
              // delete user
              ToastUtil.dialog("title", "content");
              controller.deleteFriend().then((value){
                ToastUtil.toast("删除成功");
                Routers.back();
              });
            }
          )
        ],
      );
    }
    if(controller.friendModel?.status == 2){
      // 自己是发送者
      if(controller.selfInfo.account==controller.friendModel?.creatorAccount){
        return style.optionBtn(
          "好友申请已经发送，等待对方同意",
          onTap: () async {},
        );
      }
      // 自己是接收者
      return Column(
        children: [
          style.friendSource(controller.source),
          style.attachMessage(controller.attachMessage),
          const SizedBox(height: 16,),
          style.optionBtn("同意", onTap: () async {},)
        ],
      );
    }

    return const SizedBox();
  }
}